package Aleehot100;

/**
 * ClassName: c031
 * Package: Aleehot100
 * Description: K个一组反转链表 - 25
 *
 * @Author BCXJ
 * @Create 2025/6/19 08:41
 * @Version 1.0
 * @Since 1.0
 */
public class c031 {

    // // 反转链表
    // public ListNode reverseKGroup(ListNode head, int k) {
    //     // 判断链表是否够长度
    //     ListNode end = teamEnd(head, k);
    //     if (null == end) {
    //         return head;
    //     }

    //     ListNode start = head;
    //     // 反转第一个链表快
    //     reverse(start, end);
    //     head = end;
    //     ListNode s2 = start.next;
    //     while ((end = teamEnd(s2, k)) != null) {
    //         reverse(s2, end);
    //         start.next = end;
    //         start = s2;
    //         s2 = start.next;
    //     }
    //     return head;
    // }

    // // 反转链表
    // // start -> m1 -> m2 -> end
    // // end -> m2 -> m1 -> start
    // private static void reverse(ListNode start, ListNode end) {
    //     ListNode pre = null, cur = start, next = null;
    //     end = end.next;
    //     while (cur != end) {
    //         next = cur.next;
    //         cur.next = pre;
    //         pre = cur;
    //         cur = next;
    //     }
    //     start.next = end;
    // }

    // // 找出最后一个节点
    // private static ListNode teamEnd(ListNode head, int k) {
    //     while (head != null && --k > 0) {
    //         head = head.next;
    //     }
    //     return head;
    // }

    public static void main(String[] args) {
        ListNode listNode1 = new ListNode(1);
        ListNode listNode2 = new ListNode(2);
        ListNode listNode3 = new ListNode(3);
        ListNode listNode4 = new ListNode(4);
        ListNode listNode5 = new ListNode(5);
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;

        printList(reverseKGroup(listNode1, 2));

    }


    static class ListNode {
        int val;
        ListNode next;

        public ListNode(int val) {
            this.val = val;
            next = null;
        }
    }

    // 反转链表
    public static ListNode reverseKGroup(ListNode head, int k) {
        ListNode newHead, end, start = head, pre;
        if ((end = isKLen(start, k)) == null) {
            return start;
        }
        reverse(start, end);
        start = head.next;
        pre = head;
        head = end;
        while ((end = isKLen(start, k)) != null) {
            reverse(start, end);
            pre.next = end;
            pre = start;
            start = start.next;
        }
        return head;
    }

    public static ListNode isKLen(ListNode node, int k) {
        while (node != null && k > 1) {
            node = node.next;
            k--;
        }
        return node;
    }

    /**
     * 反转链表
     *
     * @param start
     * @param end
     */
    public static void reverse(ListNode start, ListNode end) {
        ListNode endNext = end.next, cur1 = start, cur2, next = null, pre = null;
        while (cur1 != endNext) {
            next = cur1.next;
            if(cur1 != start) {
                cur1.next = pre;
            }
            pre = cur1;
            cur1 = next;
        }
        start.next = endNext;
    }


    public static void printList(ListNode node) {
        while (node != null) {
            System.out.print(node.val + " ");
            node = node.next;
        }
    }
}
